import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { memberObjType } from '@/types/member'
const tokenName = 'FS_User_token'
const identityName = {
  1: '董事长',
  2: '合伙人',
  3: '经理',
  4: '代理',
  5: '会员',
}
type identityNumberType = 1 | 2 | 3 | 4 | 5
// 定义 Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref({} as memberObjType)
    // 保存会员信息，登录时使用
    const setProfile = (val: memberObjType) => {
      profile.value = val
    }
    const setTelephone = (telephone: string) => {
      profile.value!.telephone = telephone
    }
    const setIdentity = (Identity: number) => {
      profile.value!.identity = Identity
      uni.setStorageSync('FS_User_identity', Identity)
    }
    const getIdentityName = (): string | boolean => {
      if (profile.value.identity || uni.getStorageSync('FS_User_identity')) {
        const IdentityNumber = uni.getStorageSync('FS_User_identity') as identityNumberType
        return identityName[IdentityNumber]
      }
      return false // 0表示没有登录
    }
    const setIcon = (icon: string) => {
      profile.value!.icon = icon
    }
    const setidentityIcon = (identityIcon: string) => {
      profile.value!.identityIcon = identityIcon
      uni.setStorageSync('FS_User_identityIcon', identityIcon)
    }
    const getidentityIcon = (): string | boolean => {
      if (profile.value.identityIcon || uni.getStorageSync('FS_User_identityIcon')) {
        console.log('有头像')
        console.log(profile.value.identityIcon)
        if (!profile.value.identityIcon && uni.getStorageSync('FS_User_identityIcon')) {
          profile.value.identityIcon = uni.getStorageSync('FS_User_identityIcon')
        }
        return uni.getStorageSync('FS_User_identityIcon')
      }
      return false // 0表示没有登录
    }
    const setToken = (token: string) => {
      uni.setStorageSync(tokenName, token)
      profile.value!.token = token
    }
    const setNickname = (nickname: string) => {
      profile.value!.nickname = nickname
    }
    // 清理会员信息，退出时使用
    const clearProfile = () => {
      uni.clearStorageSync()
      profile.value = {}
    }
    const getToekn = () => {
      if (profile.value.token) {
        console.log(profile.value.token)
        return profile.value.token
      } else if (uni.getStorageSync(tokenName)) {
        console.log('本地缓存中获取')
        console.log(uni.getStorageSync(tokenName))
        profile.value.token = uni.getStorageSync(tokenName)
        return uni.getStorageSync(tokenName)
      }
      console.log('没有token，返回false')
      return false
    }
    const updateBalance = (balance: string) => {
      console.log('执行了更新余额')
      console.log(balance)
      profile.value!.balance = balance
    }
    // 记得 return
    return {
      profile,
      setProfile,
      setTelephone,
      setIcon,
      setNickname,
      setIdentity,
      setToken,
      clearProfile,
      getToekn,
      updateBalance,
      getIdentityName,
      setidentityIcon,
      getidentityIcon,
    }
  },
  // TODO: 持久化
  {
    persist: {
      // 注意：默认持久化，只在H5端有效，因为默认使用的是localStorage，小程序端不能使用这个API，所以需要重写存取方法，转调给UniApp的持久化方法
      // persist: true,
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, val) {
          uni.setStorageSync(key, val)
        },
      },
    },
  },
)
